//
//  SINATecScoreQueryDetailUserNameViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/8.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecScoreQueryDetailUserNameViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINATecScoreQueryDetailViewController.h"
#import "SINATecManagerSignDetaileViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINATecCourseModel.h"

@interface SINATecScoreQueryDetailUserNameViewController ()
@property(strong,nonatomic) UIScrollView *contentScrollView;
@property (weak, nonatomic) IBOutlet UILabel *noDataLabel;
@property(strong,nonatomic) NSMutableArray *stuScoreArray;
@end

@implementation SINATecScoreQueryDetailUserNameViewController
-(UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mainW, mainH)];
        [self.view addSubview:_contentScrollView];
    }
    
    return _contentScrollView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.stuScoreArray =[[NSMutableArray alloc]init];
    DLog(@"PAGETYPE IS %@",self.pageType);
    
    if ([self.pageType isEqualToString:@"成绩查询"]) {
        [self loadingSchoolYearSubScoreListNetData];
    }else{
        //课程签到
        [self loadingSchoolSignSubScoreListNetData];
    }
    
}

-(void) initViewFunction{
    
    //每行列数
    NSInteger rank = 2;
    //每列间距
    CGFloat rankMargin = 0;
    //每行间距
    CGFloat rowMargin = 0;
    //Item索引 ->根据需求改变索引
    NSUInteger index = self.stuScoreArray.count;
    
    //每个Item宽高
    CGFloat W = (mainW-(rank-1)*rankMargin)/rank;
    CGFloat H = 45;
    NSInteger indexPageFlag = 0;
    if ([self.pageType isEqualToString:@"成绩查询"]) {
        indexPageFlag=0;
    }else{
        indexPageFlag=2;
    }
    
    for (NSInteger i = 0 ; i< index+indexPageFlag; i++) {
        
        
        //Item X轴
        CGFloat X = (i % rank) * (W + rankMargin);
        //Item Y轴
        NSUInteger Y = (i / rank) * (H +rowMargin);
        //Item top
        CGFloat top = 0;
        UIView *speedView = [[UIView alloc] init];
        speedView.backgroundColor = [UIColor whiteColor];
        speedView.frame = CGRectMake(X, Y+top, W, H);
        speedView.layer.borderWidth = 0.3;
        speedView.layer.borderColor = [[UIColor colorWithHexString:@"eeeeee"] CGColor];
        
        if ([self.pageType isEqualToString:@"成绩查询"]) {
             SINATecCourseModel *model = self.stuScoreArray[i];
            UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 21)];
            textLabel.backgroundColor=[UIColor whiteColor];
            textLabel.text=[NSString stringWithFormat:@"%@",model.realname];
            [speedView addSubview:textLabel];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:speedView andWithBlick:^(UIView *view) {
                SINATecScoreQueryDetailViewController *controller = [[SINATecScoreQueryDetailViewController alloc]initWithNibName:@"SINATecScoreQueryDetailViewController" bundle:nil];
                controller.model = model;
                [self.navigationController pushViewController:controller animated:YES];
            }];
        } else {
            
            
            /*
             
             {
             "lessonperiod": 150,
             "signcnt": 3,
             "schoolyear": "2017 - 2018",
             "term": "上半学期",
             "realname": "赵七"
             }
             */
            
            
            UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 21)];
            textLabel.text=@"";
            textLabel.font=[UIFont systemFontOfSize:16];
            [speedView addSubview:textLabel];
            
            UILabel *scroeabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15,W-30, 21)];
            scroeabel.text=@"";
            scroeabel.textAlignment=NSTextAlignmentRight;
            scroeabel.font=[UIFont systemFontOfSize:16];
            [speedView addSubview:scroeabel];
            
            if (i==0||i==1) {
                scroeabel.text=@"未到/总数";
                textLabel.text=@"姓名";
                
                textLabel.textColor=baseTextColor;
                scroeabel.textColor=baseTextColor;
            } else {
                
                SINATecCourseModel *model = self.stuScoreArray[i-indexPageFlag];
                 scroeabel.text=[NSString stringWithFormat:@"%ld/%@",[model.lessonperiod integerValue]-[model.signcnt integerValue],model.lessonperiod];
      textLabel.text=model.realname;
                textLabel.textColor=[UIColor colorWithHexString:@"#878787"];
                scroeabel.textColor=[UIColor colorWithHexString:@"#878787"];
                
                [[SINAUiviewClickLiserner alloc]viewClickFunction:speedView andWithBlick:^(UIView *view) {
                    SINATecManagerSignDetaileViewController *controller = [[SINATecManagerSignDetaileViewController alloc]initWithNibName:@"SINATecManagerSignDetaileViewController" bundle:nil];
                    //controller.model = self.courseModel;
                    controller.courseModel=model;
                    // controller.schoolYearModel = self.schoolYearModel;
                    //controller.subClassModel = self.subClassModel;
                    //[self.navigationController pushViewController:controller animated:YES];
                }];
            }
            
            
        }
        
        [self.contentScrollView addSubview:speedView];
        
    }
    self.contentScrollView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.contentScrollView.contentSize=CGSizeMake(mainW, H*(index+4)/2);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) loadingSchoolSignSubScoreListNetData{

    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //班级ID
    [dict setObject:[NSString stringWithFormat:@"%@",self.model.classifyid] forKey:@"classifyid"];
    
    
    //班级id String classifyid
    //[dict setObject:self.model.classifyid forKey:@"classifyid"];
    
    [self.stuScoreArray removeAllObjects];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getUserSignInfoUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.contentScrollView.hidden=YES;
            } else {
                //
                /*
                 
                 {
                 "lessonperiod": 150,
                 "signcnt": 3,
                 "schoolyear": "2017 - 2018",
                 "term": "上半学期",
                 "realname": "赵七"
                 }
                 */
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    
                    SINATecCourseModel *model = [[SINATecCourseModel alloc]initWithDict:dic];
                    [self.stuScoreArray addObject:model];
                    
                }
                self.noDataLabel.hidden=YES;
                self.contentScrollView.hidden=NO;
                [self initViewFunction];
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            self.noDataLabel.hidden=NO;
            self.contentScrollView.hidden=YES;
            
        }
        
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.contentScrollView.hidden=YES;
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}

-(void)loadingSchoolYearSubScoreListNetData{
 
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //班级ID
    [dict setObject:[NSString stringWithFormat:@"%@",self.model.classifyid] forKey:@"classifyid"];
    
    
    //班级id String classifyid
    //[dict setObject:self.model.classifyid forKey:@"classifyid"];
    
    [self.stuScoreArray removeAllObjects];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getClassifyUserByClassifyInfoUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.contentScrollView.hidden=YES;
            } else {
                //有班级信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    
                    SINATecCourseModel *model = [[SINATecCourseModel alloc]initWithDict:dic];
                    [self.stuScoreArray addObject:model];
                    
                }
                self.noDataLabel.hidden=YES;
                self.contentScrollView.hidden=NO;
                [self initViewFunction];
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            self.noDataLabel.hidden=NO;
            self.contentScrollView.hidden=YES;
            
        }
        
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.contentScrollView.hidden=YES;
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
